#ifndef _KNIT_H
#define _KNIT_H

#include "carriage.h"
#include "sink_setting.h"
#include "camsetting.h"

#define SCRAM     1

#define SPEED_SECT_FOR_BOARD       27
#define FLAG_YARN_IN               0x81
#define FLAG_YARN_OUT              0x82
#define FORBID_PROBE               (5)
#define INVALID_GAUZE_POS          -1000

enum {
    MAC_TYPE_NORMAL = 0,
    MAC_SINK_FOR_CAM = 1,
};

typedef  enum {SPEED_HIGH = 0, SPEED_LIMIT, SPEED_LOW, SPEED_INCH
} RUN_SPEED_TYPE;

enum { TH_STATE_PAUSE = 0, TH_STATE_RUN, TH_STATE_EXIT };
typedef enum {
    STATE_NONE = 0, STATE_RESET, STATE_RESET_STEP1, STATE_RESET_STEP2, STATE_STOP,
    STATE_RUN, STATE_RUN_SLOW, STATE_WAIT_STOP, STATE_RUN_BEFORE, STATE_KNOT
} RUN_STATE_TYPE;

enum {
    COMM_INET = 0,
    COMM_SRAM = 1,
};

enum {
    HD_ENABLE = 0,
    HD_START = 1,
    HD_END = 2,
    HD_FORBID_MODIFY = 3,
};

enum {
    RESET_NORMAL = 0,
    RESET_GOTO = 1,
    RESET_HD = 2,
    RESET_DROP = 3,
};

enum {
    ENC_EOK = 0,
    ENC_ENOMEM,
    ENC_EOFF,
    ENC_EFS,
    ENC_EFRD,
    ENC_EFNM,
    ENC_ESTF,
    ENC_EOLR,
    ENC_ENE,
    ENC_EPTL,
    ENC_EWR,
    ENC_ECHK,
    ENC_EINVH,
    ENC_EUSAGE
};



enum {
    NEED_DO_NONE = 0,
    NEED_DO_RESET_START = 1,
    NEED_DO_GOON_KNIT = 2,
    NEED_DO_WAIT_RESET = 3,
};

#define AUTO_DROP_FLAG               0xAA55
enum {
    PK_AUTO_BOARD = 0,
    PK_AUTO_HD_DROP = 1,
    PK_AUTO_DROP = 2,
};
typedef struct _SEND_LINE     SEND_LINE, *PSEND_LINE;
typedef struct _SYS_LOG   SYS_LOG, *PSYS_LOG;

struct _SYS_LOG {
    int code;
    int type;
    union {
        uint time;
        uint total;
    };
    int rsvd;
    struct _SYS_LOG *next;
};

typedef enum {
    KNIT_PROCESS_NONE = 0,
    KNIT_PROCESS_RESET_YARN = 1,
    KNIT_PROCESS_RESET_SELECT = 2,
    KNIT_PROCESS_RESET_SINK = 3,
    KNIT_PROCESS_RESET_WAIT_HANDLE = 4,
    KNIT_PROCESS_RESET_CAM = 5,
    KNIT_PROCESS_RESET_STITCH = 6,
    KNIT_PROCESS_RESET_FINISHED = 7,//wait for line 2's data
    KNIT_PROCESS_RUN_BEFORE = 8,
    KNIT_PROCESS_NORMAL_RUN = 9, //wait for line 3's data
    KNIT_PROCESS_RESET_ROCK = 10,
    KNIT_PROCESS_RESET_BOARD = 11,
    KNIT_PROCESS_OPEN_RAB = 12,
    KNIT_PROCESS_DROP_CLOTH = 13,
}KNIT_PROCESS;


typedef enum
{
    USAGE_F2812 = 0x01,
    USAGE_F2407 = 0x02,
    USAGE_F2802 = 0x03,
    USAGE_F2812_NET = 0x04,
    USAGE_F2407_SSDC = 0x05,
    USAGE_F2407_DSDC = 0x06,
    USAGE_F2407_SSSC = 0x07,
    USAGE_STM32_GAUZE = 0x08,
    USAGE_STM32_DSCRG = 0x09,
} usage_t;

typedef struct _ID
{
    uint secs;
    uint comp:8;
    uint times:6;
    uint days:10;
    uint times_h:8; /* 次数的高8位（后来扩展） */
} ID;
typedef struct _BOARD_CONFIG
{
    ushort signature; // MH
    ushort version;   // 版本号
    uchar unid[8];    // 产品唯一ID
    uint set_time;    // MH出厂时间限定

    ID id;            // 客户设置
    uchar give;       // 赠送时间标记
    uchar rsvd1[7];

    uint total_time;  // 总累计时间
    uint acc_time;    // 解锁后的累计时间
    uint last_time;   // 最后更改时间
    uint chksum;      // 校验和
} BOARD_CONFIG;

struct _SYS_ID {
    uchar id[16];
    int time_left;
};
typedef struct _SYS_ID    SYS_ID, *PSYS_ID;

typedef struct _BACKPIECE BACKPIECE;
typedef struct _PAT_RANGE PAT_RANGE;

struct _PAT_RANGE
{
    int pat_start;
    int pat_end;
};

struct _BACKPIECE
{
    int start_row;      /* start from 1 */
    int end_row;        /* 后片的起始行和结束行 */

    PAT_RANGE *prange;

    int fp_end_row;
};

typedef struct _PK_DATA     PK_DATA;
typedef struct _RUN_PK_DATA RUN_PK_DATA;
typedef struct _CTRL_PAGE   CTRL_PAGE;

struct _PK_DATA
{
    int rows;
    unsigned char *buf;
};

struct _RUN_PK_DATA
{
    int pat_width;
    int type;       /* 0-起底板；1-自动行定落布；2-卸片 */
    PK_DATA pcnt;
    PK_DATA ppat;
};

struct _CTRL_PAGE
{
    short sub;            //0,主程式；1,副程式
    short row;            //在主程式或者副程式中的行数(0开始)
    short threhold;       //阈值，针数超过这个阈值，摇床就可以动第二次
    short limit_speed;
    KNIT_PAGE *pcon;
    ushort src;           //压缩前的行(1开始)
    ushort rsvd;          //start from 1
};

typedef struct
{
    ushort start;
    ushort end;
    ushort num;
} ECONOMY_CLASS;

typedef struct tagEco
{
    ECONOMY_CLASS node;
    struct tagEco *next;
} ECONOMY_LIST;

typedef struct
{
    ushort rows;
    KNIT_PAGE *pcon;
}AUX_CON;

typedef struct tagAuxList
{
    ushort num;
    ushort line;
    int index;
    AUX_CON *paux_con;
}AUX_LIST;

typedef struct {
    KNIT_PAGE *knit_page_head;
    KNIT_PAGE *knit_page;
    char file_name[256];
    ushort curt_line;//0 start
    ushort total_line;

    uchar rock_low_speed;
    uchar cam_lock;
    uchar dumu_work_delta;
    uchar dumu_reset_delta;

    uchar net_state;
    uint total_cloth;
    uint done_cloth;
    uint run_time;//for after start
    uint knit_time;//for one piece
    uint knit_count_time;//count time
    int needle_zero;
    int left_yarn_right_com;
    int left_yarn_left_com;
    int left_forbid;
    int right_forbid;
    float inch_type;

    uchar run_state;
    uchar run_reset_state;
    uchar bar_state;

    uchar select_sect;
    uchar cam_high_voltage;
    uchar select_high_voltage;
    uchar select_low_voltage;

    uchar save_screen_time;
    uchar save_screen_enable;
    uchar mac_driver_enable;
    uchar signal_light_state;
    uchar mac_dir;

    int select_right_com;
    int select_left_com;

    uchar dumu_reset_speed;
    uchar dumu_work_speed;
    ushort total_needle;

    int belt_left_com;
    int belt_right_com;
    int belt_pitch_adjust;
    int belt_curt_pulse;

    uchar rock_speed;
    uchar sink_enable;
    uchar language;
    uchar select_repeat_enable;

    uchar mac_out_enable;
    uchar rock_type;
    uchar rab_percent;
    uchar mac_low_speed;

    uchar mac_reset_speed;
    uchar mac_mid_speed;
    uchar mac_high_speed;
    uchar mac_high_line_speed;

    ushort start_needle;
    uchar machine_ok;
    uchar rock_mid_speed;

    int rock_gap_com;
    int roll_open_com;
    int yarn_before;

    uchar rab_stop_torque;
    uchar mac_ret_len;
    ushort auto_zero_cloth;

    int back_sink_reset_value;
    int front_sink_reset_value;

    uchar mac_drt;
    uchar shake_enable;
    uchar front_shake_sense;
    uchar back_shake_sense;

    uchar front_safe_door_enable;
    uchar back_safe_door_enable;
    uchar gauze_type;                    //纱嘴控制方式
    uchar board_enable;

    uchar scissor_enable;
    uchar rab_relax_for_stop;
    ushort economy_left;

    uchar progress_percent;
    char  grogress_name[20];

    uchar yarn_take_mode;
    uchar auto_line_set;
    uchar return_state;
    uchar copy_rab_enable;

    ushort economy_start;
    ushort economy_end;
    ushort economy_times;
    uchar probe_shield;                   //探针屏蔽距离(mm)
    uchar dumu_add_percent;               //停车度目增加

    ECONOMY_LIST *peco_list;

    short rock_right_star_com[16];   //右针对针位
    short rock_right_com[16];           //右总针位
    short rock_left_star_com[16];       //左针对针位
    short rock_left_com[16];                //右总针位
    short rock_right_tran_com[16];      //右翻针位
    short rock_left_tran_com[16];       //左翻针位
    int dmbz_single_com[2][8];
    uchar gauze_exchange[2][16];

    ushort flower_width;
    ushort flower_height;
    uchar *flower_data;

    uchar light_state;
    uchar driver_enable;
    uchar mac_test_speed;
    uchar cam_type;                 //三角控制方式

    uchar rab_test_speed;
    uchar rab_test_open;
    uchar roll_test_speed;
    uchar roll_test_open;

    uchar left_feeder_test;
    uchar right_feeder_test;
    uchar mac_driver_warn_sign;
    uchar mac_driver_ready_sign;

    uchar rock_driver_warn_sign;
    uchar rock_driver_ready_sign;
    uchar handle_quick_sign;
    uchar handle_stop_sign;

    uchar handle_slow_sign;
    uchar handle_scram_sign;
    uchar left_forbid_sign;
    uchar right_forbid_sign;

    uchar rock_forbid_sign;
    uchar rock_zero_sign;
    uchar left_grab_sign;
    uchar right_grab_sign;

    uchar balcony_break_sign;
    uchar balcony_knot_sign;
    uchar balcony_big_knot_sign;
    uchar drop_bad_sign;

    uchar front_shake_sign;
    uchar back_shake_sign;
    uchar needle_zero_sign;
    uchar mac_code_sign;

    uchar rock_code_sign;
    uchar key_code_sign;
    uchar left_back_safe_door_sign;
    uchar right_back_safe_door_sign;

    uchar front_rab_warn_sign;
    uchar back_rab_warn_sign;
    uchar roll_warn_sign;
    uchar roll_bad_sign;

    uchar rab_open_sign;
    uchar roll_open_sign;
    uchar left_feeder_warn_sign;
    uchar right_feeder_warn_sign;

    char rab_speed[32];//high roller//rab
    uchar roll_speed[32];//low roller//aux rab
    uchar switch_speed[32];
    uchar mac_speed[32];
    ushort dumu_value[2][8][32];
    ushort yarn_stop[2][8][16];
    ushort sink_value[10][8];
    int sink_focus;
    ushort cam_value[4][8];
    int cam_focus;
    short dumu_before_com[2][32];      //0-in,1-out
    int dmbz_yarn_com[2][8];
    uchar rude_dumu[32];               //二段度目
    int yarn_stop_com[134];

    short dumu1_to_mac_left_len;
    short dumu2_to_mac_left_len;
    short dumu3_to_mac_left_len;
    short dumu4_to_mac_left_len;

    short sink1_to_mac_left_len;
    short sink2_to_mac_left_len;

    ushort ext_piece_inter;
    ushort ext_piece_width;

    uchar ext_piece_yarn[10][16];

    uchar ext_piece_enable[10];
    uchar left_feeder_enable;
    uchar right_feeder_enable;
    uchar feeder_speed[32];

    ushort goto_line;
    ushort stop_line;

    uchar piece_lock;
    uchar yarn_up;
    uchar warn_enable;
    uchar gui_speed_type;//speed_switch

    uchar hangding_enable;
    uchar hd_start_base;
    uchar hd_end_base;
    uchar hangding_times;

    uchar file_type;
    uchar task_finished;
    uchar rab_speed_for_stop;
    uchar roll_speed_for_stop;

    uchar switch_speed_for_stop;
    uchar ext_piece_num;
    uchar left_yarn_feeder_state;
    uchar right_yarn_feeder_state;
    ushort rock_needle;

    int error_code;
    struct MACHINE *pmac;
    HEAD_TEST head_test_backup;
    uchar yarn_state[4];           //down = 1, up = 0
    uchar cam_state[2][4];
    uchar select_state[2][8][8];
    int mac_focus;
    int mac_wnd;
    short rock_pos;//pulse not silk
    int rock_pos_src;
    short rock_star;
    time_t sys_time;
    short select1_to_mac_left_len;
    short select2_to_mac_left_len;
    short select3_to_mac_left_len;
    short select4_to_mac_left_len;
    ///////////////////////////////////////////mac_param  data
    //short select_piece;          //define before "select_sect"
    short select_mode;
    short rock_mode;
    short rock_zero_dir;
    short red_light_time;
//    short select_sect;               //define before
    short sever_type;
    short probe_to_mac_left_len;
    short select_start;
    short yarn_type;
    short sink_dir;
    //   short dumu1_to_mac_left_len;              //define before
  //  short dumu2_to_mac_left_len;                 //define before
  //  short dumu3_to_mac_left_len;                 //define before
  //  short dumu4_to_mac_left_len;                 //define before
    short fuel_gab;
    short fuel_time;
    short select5_to_mac_right_len;
    short select6_to_mac_right_len;
    short mac_width;

    MOTOR_DRIVE motor_drive;
    ROCK_DRIVE rock_drive;

    short dumu_state[2][8];
    short sink_state[2][4];

    uchar cam_low_voltage;
    uchar dumu_recv_com;
    uchar dumu_before_enable;
    uchar sink_work_speed;

    ushort feeder_start_wait;
    ushort feeder_stop_wait;

    uchar dumu_reset_warn_enable;
    uchar run_special_enable;
    uchar second_dumu_enable;
    char second_dumu_com;               //二段度目针数

    uchar roll_stop_torque;
    uchar knot_wait_line;
    uchar rab_run_type;
    uchar select_sect_start;

    uchar company;
    uchar param_change;
    uchar need_do_which;
    uchar gauze_high_voltage;

    char dumu_sensor[8];
    char sink_sensor[2];

    int right_yarn_right_com;
    int right_yarn_left_com;
    uchar select[8];

    uchar communication;
    uchar run_knot;
    uchar th_request;
    uchar th_state;

    pthread_t th_run;

    struct
    {
        uint cam : 3;            //1开始,1-1系统,2-2系统
        uint motor : 12;
        uint lock : 1;
        /*
         * bit0-度目前1
         * bit1-度目前2
         * bit2-度目后1
         * bit3-度目后2
         * bit4-前辛克
         * bit5-后辛克
         */
        uint gauze : 8;
        uint mac : 6;
        uint rock_compress : 1;
        uint rsvd : 1;
    } sys_info;

    struct
    {
        uchar clip_off[4];
        uchar scissor[2];

        uchar hook_open : 1;
        uchar hook_close : 1;
        uchar board_safe : 1;
        uchar board_high : 1;
        uchar board_zero : 1;
        uchar rsvd1 : 3;

        uchar rsvd2;

        short coder : 1;
        short rsvd3 : 1;
    }board_state;

    uchar mac_speed_switch;
    uchar sram_need_send;
    uchar sys_state;
    uchar tran_cam_state;

    ushort last_line;
    ushort inch_pulse;

    struct{
        uchar enable : 1;
        uchar rsvd : 5;
        uchar right_forbid : 1;
        uchar left_forbid : 1;
    }header_type;    //双机头

    uchar dumu_com;       //度目补偿
    uchar mac_empty_speed;//空跑速度
    uchar yarn_feeder_mode;

    struct {
        ushort yarn_left_out;
        ushort yarn_right_out;
        uchar stop[2][16];
    }yarn_out;

    short yarn_before_com[2][2][32];/* l0r1, u0d1, speed for each 5 */
    short mac_before_com[2][32];    /* l0r1, speed for each 5 */

    ushort rab_cell_rsvd;
    ushort need_loop;
    struct {
        uint reset_finished : 1;
        uint drt : 1;
        uint process : 5; //only from KNIT_PROCESS
        uint lock_read_error : 1;
        uint bp_enable : 1;/*only back piece*/
        uint fp_enable : 1;/*only front piece*/
        uint auto_hd_drop : 1;
        uint prefix : 1;/*knitting prefix*/
        uint forbid_feedback : 1;
        uint stop_line : 1;
        uint hd_wait : 1;
        uint hd_forever;
        uint take_yarn_mode : 1;
        uint tran_dumu_enable : 1;
        uint rock_zero_drt : 1;
        uint sink_drt : 1;
        uint rab_enable : 1;//高罗拉使能
        uint roll_enable : 1;//低罗拉(主拉布)使能
        uint goon : 1;
        uint drop_cloth : 1;
        uint rsvd : 8;
    }knit_state;

    int fper_pat_start;     //front piece end row pat start
    int fper_pat_end;
    int bper_pat_start;
    int bper_pat_end;
    int left_bound;
    int right_bound;
    int knit_start;
    int knit_end;
    int sp;
    uint total_cloth_backup;
    uint done_cloth_backup;
    ushort need_loop_record;
    ushort backpiece_start_line;

    ushort hangding_start;
    ushort hangding_end;

    uint mac_header_inter;                //机头间距(mm)
    ushort yarn_inter_4cam;               //左右系统纱嘴电磁铁距离(mm)
    struct {
        ushort select_array : 1;          //针脚排列
        ushort clip_enable : 1;           //与左夹子有效共用
        ushort right_clip_enable : 1;     //右夹子使能
        ushort dumu_sprt : 1;             //度目分次动作
        ushort driver_type : 1;           //伺服类型(CAN)
        ushort rsvd : 11;
    }mac;

    struct {
        short last_needle;                //前1次机头位置
        ushort wait_stop : 15;            //等待机头停下来
        ushort off : 1;                   //断电
    }power;

    short dumu_max_value;                //密度马达最大行程
    short probe_distance;                //探针到右边沿距离(mm)

    ushort rab_max_param;                //高罗拉最高速
    ushort rab_min_param;                //高罗拉最低速

    uint alarm_mask;                      //屏蔽掩码

    struct {
        uint yarn : 6;
        uint mode : 3;
        uint rsvd : 23;
        int yarn_left;
        int yarn_right;
    }st_board;

    uint mac_num;                        //机器编号

    struct {
        int high_pos;                     //最高位(勾线位)
        int safe_pos;                     //安全位
        int pos_for_roll_close;           //罗拉合上位置
        int hook_open_pos_for_roll;       //脱圈位(高罗拉)
        int delay_for_hook_open;          //脱圈位滞后
        int speed_for_up_start;           //起底板上升初始速度
        int speed_for_up_quick;           //起底板上升加速速度
        int speed_for_up_slow;            //起底板上升减速速度
        int speed_for_drop_2safe_pos;     //降到安全位速度
        int reset_speed;                  //起底板复位速度
        int pos_for_down_slow;            //起底板下降减速位置
        int speed_for_down_slow;          //起底板下降减速速度
        int speed_for_roll_when_drop;     //落布时罗拉速度
        int wait_for_drop;                //落布等待时间
        short board_type;
        short pos_for_up_wait;            //起底板上升等待位置
        int hook_open_pos_for_rab;        //脱圈位2(主拉布)
        short rab_close_speed;            //主拉布合上速度
        short rab_open_speed;             //主拉布打开速度
        int pos_for_rab_close;            //主拉布合上位置
        short pos_for_up_quick;           //起底板上升加速位置
        short pos_for_up_slow;            //起底板上升减速位置
        int speed_down_after_hook_open;   //起底板下降脱圈速度
        uchar balance;                    //平衡值
        uchar scissor_times;              //剪刀次数
        uchar start_knit_mode;            //起底模式
        uchar gauze;                      //起底纱嘴

        uchar pull;
        uchar cs_syn;                     //剪夹联动
        uchar rsvd2;
        uchar rsvd3;
    }board_param;

    AUX_LIST aux_list;                    //副程式
    HWND pg_hwnd;

    uchar sram_send_data[SEND_MAX_SIZE];
    RUN_FEEDBACK run_feedback;
    SYS_LOG log_list;
    BOARD_CONFIG payment;
    BACKPIECE backpiece;
    RUN_PK_DATA pk_data;
    CTRL_PAGE *ctrl_page;
    CARRIAGE *crg;
    SINK_SETTING *sink_set;
    CAM_SETTING *cam_set;
    short yarn_set[12][100];

}MH_KNIT;



#pragma pack(1)
struct _SEND_LINE {
    uchar head[8];
    struct {
        uchar g1in : 1;
        uchar g1out : 1;
        uchar state : 6;
    }cam_1F;                    //0
    struct {
        uchar clip_1off : 1;
        uchar clip_2off : 1;
        uchar state : 6;
    }cam_1B;                    //1
    struct {
        uchar g2in : 1;
        uchar g2out : 1;
        uchar state : 6;
    }cam_2F;                   //2
    struct {
        uchar clip_3off : 1;
        uchar clip_4off : 1;
        uchar state : 6;
    }cam_2B;                   //3
    struct {
        uchar g3in : 1;
        uchar g3out : 1;
        uchar state : 6;
    }cam_3F;                   //4
    struct {
        uchar clip_1on : 1;
        uchar clip_2on : 1;
        uchar state : 6;       //5
    }cam_3B;
    struct {
        uchar g4in : 1;
        uchar g4out : 1;
        uchar state : 6;      //6
    }cam_4F;
    struct {
        uchar clip_3on : 1;
        uchar clip_4on : 1;
        uchar state : 6;      //7
    }cam_4B;
    uchar curt_gauze1_1;           //8(high bit for gauze1)
    uchar curt_gauze2_1;           //9
    uchar next_gauze1_1;           //10
    uchar next_gauze2_1;           //11
    short rock_pulse;              //12-13
    short third_end;               //14-15
    char rock_needle;              //16
    struct {
        uchar off1 : 1;
        uchar on1 : 1;
        uchar off2 : 1;
        uchar on2 : 1;
        uchar off3 : 1;
        uchar on3 : 1;
        uchar off4 : 1;
        uchar on4 : 1;
    }clip;                         //17
    struct {
        uchar scissor1 : 1;
        uchar scissor2 : 1;
        uchar qdb_raise : 1;
        uchar drop_cloth : 1;
        uchar lock_mac : 1;
        uchar disable_run : 1;
        uchar enable_run : 1;
        uchar board_restart : 1;
    }board;                      //18
    short rock_shake;            //19-20
    uchar pause;                 //21
    short plate_pulse_2rock;     //22-23
    struct {
        uchar f1 : 1;
        uchar f2 : 1;
        uchar b1 : 1;
        uchar b2 : 1;
        uchar rsvd : 4;
    }select_repeat;              //24
    uchar rsvd1[3];              //25-27
    uchar curt_gauze1_9;         //28(high bit for gauze9)
    uchar curt_gauze2_9;         //29
    uchar next_gauze1_9;         //30
    uchar next_gauze2_9;         //31
    short threhold;              //32-33
    uchar rsvd2[6];              //34-39
    short dumuF1;                //40-41
    short dumuB1;                //42-43
    short dumuF2;                //44-45
    short dumuB2;                //46-47
    short dumuF3;                //48-49
    short dumuB3;                //50-51
    short dumuF4;                //52-53
    short dumuB4;                //54-55
    short rsvd3[8];              //56-71
    short sinkF1;                //72-73
    short sinkB1;                //74-75
    short rsvd4[2];              //76-79
    struct {
        uchar left_head_disable : 1;
        uchar right_head_disable : 1;
        uchar rsvd : 6;
    }double_head;                //80
    uchar board_pull;            //81
    uchar motor_speed;           //82
    uchar low_roller;            //83
    struct {
        uchar rsvd1 : 2;
        uchar allow : 1;
        uchar probe : 1;
        uchar rsvd2 : 4;
    }forbid;                     //84
    uchar kaihe;                 //85
    short stop1;                 //86-87
    short stop2;                 //88-89
    short stop3;                 //90-91
    short stop4;                 //92-93
    short stop5;                 //94-95
    short stop6;                 //96-97
    short stop7;                 //98-99
    short stop8;                 //100-101
    uchar ret_len;               //102
    uchar rock_speed;            //103
    short yarn_before_down;      //104-105
    short yarn_before_up;        //106-107
    uchar low_speed;             //108
    short low_yarn_before_down;  //109-110
    short low_yarn_before_up;    //111-112
    int high_roller_freqency;    //113-116
    uchar roll_percent;          //117
    struct {
        uchar yarn_up : 1;
        uchar yarn_down : 1;
        uchar stop_bit : 1;
        uchar zero_bit : 1;
        uchar stop_line : 1;
        uchar inch_run : 1;
        uchar rab_run : 1;
        uchar mute : 1;
    }opcode;                     //118
    uchar left_feeder_speed;     //119
    uchar right_feeder_speed;    //120
    ushort inch_pulse;           //121-122
    short high_roller_speed;     //123-124
    uchar mac_before;            //125
    uchar mac_low_before;        //126
    uchar high_roll_open;        //127
    short stop9;                 //128-129
    short stop10;                //130-231
    short stop11;                //132-133
    short stop12;                //134-135
    short stop13;                //136-137
    short stop14;                //138-139
    short stop15;                //140-141
    short stop16;                //142-143
    uchar dumu_add_percent;      //144
    uchar probe_capacity;        //145
    uchar dumu_slow_in_com;       //146
    uchar dumu_slow_out_com;      //147
    uchar dumu_high_in_com;       //148
    uchar dumu_high_out_com;      //149
    uchar rude_capacity;         //150
    short rude_dumu;             //151-152
    uchar dumu_stop_in_com;      //153
    uchar dumu_stop_out_com;     //154
    uchar rsvd7[125];            //155-279
    uchar flw[1600];
};

#pragma pack()

void GetNormalKnitData(MH_KNIT *p, uchar *data, ushort line, int prefix, int aux_index);

/*
 *
 */
int InitPayment(MH_KNIT *p);
/*
 * get control page from data
 * param n: knit line, 1-start
 */
KNIT_PAGE* GetControlPage(MH_KNIT *p, ushort n);
/*
 * get flower line from knit data
 */
uchar* GetFlowerLine(MH_KNIT *p, ushort n);
/*
 * get width of flower
 */
ushort GetFlowerWidth(MH_KNIT *p);
/*
 * get height of flower
 */
ushort GetFlowerHeight(MH_KNIT *p);
/*
 * get which dumu for sect
 */
ushort GetRunDumu(MH_KNIT *p, uint f0b1, uint sect, uint which);
/*
 * get single dmbz for n
 */
int GetSingleDmbz(MH_KNIT *p, uint f0b1, uint which);
/*
 * get speed of yarn feeder
 * param sect: start from 0 to 31
 */
uchar GetFeederRunSpeed(MH_KNIT *p, uint sect);
/*
 * get speed of rab for sect
 */
char GetRabRunSpeed(MH_KNIT *p, uint sect);
/*
 *
 */
float GetRockPulsePerSilk(MH_KNIT *p);
/*
 *
 */
uchar GetRockType(MH_KNIT *p);
/*
 * get next run line
 * param line: start from 1
 * ret: start from 1
 */
int GetNextLine(MH_KNIT *p, int line, int *prefix, int *aux_index);
/*
 * get which yarn stop for sect
 */
uchar GetRunYarnStop(MH_KNIT *p, uint l0r1, uint sect, uint which);
/*
 * get speed of roll for sect
 */
uchar GetRollRunSpeed(MH_KNIT *p, uint sect);
/*
 * get speed of switch for sect
 */
uchar GetSwitchRunSpeed(MH_KNIT *p, uint sect);
/*
 * get speed of machine for sect
 * param sect: start from 0 to 31
 */
uchar GetMacRunSpeed(MH_KNIT *p, uint sect);
/*
 * get start of hangding
 */
int ResetMachine(MH_KNIT *p, int start_line, int type);
/*
 *
 */
float GetRockPulsePerSilk(MH_KNIT *p);
/*
 *
 */
int GetMotorPulsePerMeter(MH_KNIT *p);
/*
 * run rock
 */
int RunRock(MH_KNIT *p, short silk);
/*
 *
 */
int ResetRock(MH_KNIT *p, short silk);
/*
 *
 */
void RunChangeParam(MH_KNIT *p);
/*
 *
 */
void GetChangedParam(MH_KNIT *p, uchar *data, ushort next_line, int prefix, int aux_index);
/*
 *
 */
int SetRunThreadState(MH_KNIT *p, int state);
/*
 *
 */
void AddSysLog(MH_KNIT *p, int type, int error);
/*
 *
 */
int PowerOff(MH_KNIT *p);
/*
 *
 */
uchar GetWhichNeedDo(MH_KNIT *p);
/*
 *
 */
int GetHighRollerFrequency(MH_KNIT *p, int speed);
/*
 *
 */
short GetRockZeroCom(MH_KNIT *p);
/*
 *send mac param to machine
 */
int SendPartsParam(MH_KNIT *p);
/*
 * send system param to machine
 */
int SendSysParam(MH_KNIT *p);
/*
 * send system param to machine
 */
int SendAlarmMask(MH_KNIT *p);
/*
 *
 */
int GetAlarmMask(MH_KNIT *p);
/*
 * set time for screen saver
 */
void Ts485SetScreenSaver(ushort time_set_sec);
/*
 * get rude of dumu for sect
 * param sect: start from 0 to 31
 */
uchar GetRudeDumu(MH_KNIT *p, uint sect);
/*
 *
 */
int SwitchLockCam(KNIT_PAGE *pcon, uchar lock, int l0r1);
/*
 *
 */
ushort GetYarnLeftOut(MH_KNIT *p);
/*
 *
 */
ushort GetYarnLeftOutStop(MH_KNIT *p);
/*
 *
 */
ushort GetYarnRightOut(MH_KNIT *p);
/*
 *
 */
ushort GetYarnRightOutStop(MH_KNIT *p);
/*
 *
 */
void SafeFreeAuxConList(MH_KNIT *p);
/*
 * line: start from 0
 */
int GetAuxConIndex(MH_KNIT *p, ushort line);
/*
 * line: start from 1
 */
void GetAuxConPage(MH_KNIT *p, KNIT_PAGE *pcon, int index, ushort line);
/*
 * get version info
 */
int GetVersionInfo(MH_KNIT *p, VERSION *pversion);

#endif
